<?php

use Illuminate\Database\Seeder;

/**
 * user_detail 写入 union_id
 * Class UserDetailTableSeeder
 */
class UserDetailTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * 通过微信open_id 获取unionid 写入数据库
     * @return string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function run()
    {
        $data  =  $user_detail = \App\Model\UserDetail::get();
        foreach ($data as $key => $datum){
            $user_info = \App\Model\WeChat::getWeChatUser ($datum->open_id);
            $subscribe = isset($user_info["subscribe"]) ? $user_info["subscribe"] :0 ;
            $detail = \App\Model\UserDetail::whereId($datum->id)->first();
            $detail->union_id = isset($user_info["unionid"]) ? $user_info["unionid"] : null;
            $detail->subscribe = $subscribe;
            $detail->save();
        }
        return '修改成功！';
    }
}
